﻿using db_manage.dll.common;
using db_manage.dll.manage;
using System;
using System.Windows.Forms;

namespace db_manage.page
{
    public partial class form_main : Form
    {
        private form_db_config form_db_config { get => manage.from_get<form_db_config>(); }
        private form_exec form_exec { get => manage.from_get<form_exec>(); }
        private form_db_info form_db_info { get => manage.from_get<form_db_info>(); }
        private form_bak form_bak { get => manage.from_get<form_bak>(); }
        private form_detach_attach form_detach_attach { get => manage.from_get<form_detach_attach>(); }
        private form_diff form_diff { get => manage.from_get<form_diff>(); }

        public form_main()
        {
            InitializeComponent();
            if (manage.db_manage_config.db_config_bak.enable)
            {
                form_bak.db_bak();
            }
        }

        /// <summary>
        /// 数据库配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_db_config_Click(object sender, EventArgs e)
        {
            form_db_config.ShowAndBringToFront();
        }

        /// <summary>
        /// 分离附加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_detach_attach_Click(object sender, EventArgs e)
        {
            form_detach_attach.ShowAndBringToFront();
        }

        /// <summary>
        /// 脚本执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_exec_Click(object sender, EventArgs e)
        {
            form_exec.ShowAndBringToFront();
        }

        /// <summary>
        /// 备份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_bak_Click(object sender, EventArgs e)
        {
            form_bak.ShowAndBringToFront();
        }

        /// <summary>
        /// 数据库差异处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_diff_Click(object sender, EventArgs e)
        {
            form_diff.ShowAndBringToFront();
        }

        /// <summary>
        /// 数据库列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_db_list_Click(object sender, EventArgs e)
        {
            form_db_info.ShowAndBringToFront();
        }

        /// <summary>
        /// 缩小为托盘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void form_main_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("是否缩小到托盘？", "退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                //取消关闭窗口
                e.Cancel = true;
                //最小化主窗口
                this.WindowState = FormWindowState.Minimized;
                //不在系统任务栏显示主窗口图标
                this.ShowInTaskbar = false;
                //提示气泡
                notifyIcon1.ShowBalloonTip(2000, "最小化到托盘", "程序已经缩小到托盘，单击打开程序。", ToolTipIcon.Info);
            }
            else if (manage.db_manage_config.db_config_bak.enable)
            {
                if (MessageBox.Show("检测到程序启用了数据库备份功能，是否确认退出？", "退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                {
                    e.Cancel = true;
                }
            }
        }

        /// <summary>
        /// 托盘还原
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    //还原窗体
                    this.WindowState = FormWindowState.Normal;
                    //系统任务栏显示图标
                    this.ShowInTaskbar = true;
                }
                //激活窗体并获取焦点
                this.Activate();
            }
        }
    }
}
